package openase.audio.fingerprinthash;

import openase.audio.AudioFingerprint;
import openase.audio.Storage;
import openase.dsp.FrequencyDomain;
import openase.dsp.TimeDomain;
import openase.dsp.conversion.DefaultTimeDomainToFrequencyDomainConverterImpl;
import openase.dsp.conversion.TimeDomainToFrequencyDomainConverter;

public class FingerprintGeneratorFacade {

	public static AudioFingerprint[] generateFingerprints(byte[] timeDomainData, int chunkSize) {
		final TimeDomain td = new TimeDomain(timeDomainData);
		final FrequencyDomain[] frequencies = getTd2FdConverter().convert(td, chunkSize);

		final AudioFingerprint[] fps = new AudioFingerprint[frequencies.length];

		final FingerprintHashGenerator fhg = FingerprintHashGenerator.getDefault();

		final Storage storage = Storage.getStorage();

		for (int i = 0; i < frequencies.length; i++) {
			final long hash = fhg.generate(frequencies[i]);
			AudioFingerprint fp = storage.newFingerprint(i, hash);
			fps[i] = fp;
		}

		return fps;
	}

	private static TimeDomainToFrequencyDomainConverter getTd2FdConverter() {
		return new DefaultTimeDomainToFrequencyDomainConverterImpl();
	}

}
